<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>题解 AcWing 2236. 伊基的故事 I - 道路重建 | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="网络瘤">
<meta property="og:type" content="article">
<meta property="og:title" content="题解 AcWing 2236. 伊基的故事 I - 道路重建">
<meta property="og:url" content="http://example.com/2022/05/18/%E9%A2%98%E8%A7%A3%20AcWing%202236.%20%E4%BC%8A%E5%9F%BA%E7%9A%84%E6%95%85%E4%BA%8B%20I%20-%20%E9%81%93%E8%B7%AF%E9%87%8D%E5%BB%BA/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="网络瘤">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2022-05-18T08:32:04.560Z">
<meta property="article:modified_time" content="2023-07-19T11:50:50.850Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
  
<meta name="generator" content="Hexo 6.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"><span class="fa fa-bars"></span></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
        
          <a class="nav-icon" href="/atom.xml" title="RSS Feed"><span class="fa fa-rss"></span></a>
        
        <a class="nav-icon nav-search-btn" title="Search"><span class="fa fa-search"></span></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://example.com"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-题解 AcWing 2236. 伊基的故事 I - 道路重建" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/2022/05/18/%E9%A2%98%E8%A7%A3%20AcWing%202236.%20%E4%BC%8A%E5%9F%BA%E7%9A%84%E6%95%85%E4%BA%8B%20I%20-%20%E9%81%93%E8%B7%AF%E9%87%8D%E5%BB%BA/" class="article-date">
  <time class="dt-published" datetime="2022-05-18T08:32:04.560Z" itemprop="datePublished">2022-05-18</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      题解 AcWing 2236. 伊基的故事 I - 道路重建
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <p>网络瘤</p>
<p><a target="_blank" rel="noopener" href="https://www.acwing.com/problem/content/2238/">$\tt{Acwing题库链接}$</a></p>
<h3 id="算法-1-（暴力）"><a href="#算法-1-（暴力）" class="headerlink" title="算法 1 （暴力）"></a>算法 1 （暴力）</h3><p>先对原图跑一遍最大流，再依次增加每条边的容量后重新求最大流，如果增加某条边的容量后最大流变大了，说明它就是关键边。</p>
<p>这个智障东西的时间复杂度大概是 $O(n^2 m^2)$ 的（如果使用 Dinic 求最大流），显然需要优化。</p>
<h3 id="算法-2"><a href="#算法-2" class="headerlink" title="算法 2"></a>算法 2</h3><p>真的需要跑 m 次最大流吗？</p>
<p>其实不需要。对原图跑一次最大流之后，我们其实已经获得了一些有用的信息————原图的残量网络。注意到这样一个性质，在原图上增加某条边的容量后最大流变大时，在残量网络上增加这条边的容量后应该会存在一条从源点到汇点的增广路。</p>
<p>所以我们可以想出这样一个做法：先对原图跑一次最大流，求出原图的残量网络；再依次增加残量网络上每条边的容量，判断增加容量后是否存在从源点到汇点的增广路，如果存在说明它就是关键边。</p>
<p>这样搞的时间复杂度应该是 $O(n^2 m)$ 的（瓶颈变成了 Dinic），足够通过此题。</p>
<p>代码：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;queue&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;algorithm&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;bitset&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="type">const</span> <span class="type">int</span> INF=<span class="number">0x3fffffff</span>;</span><br><span class="line"><span class="type">int</span> n,m;</span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">edge</span>&#123;</span><br><span class="line">    <span class="type">int</span> v,w,nxt,num;</span><br><span class="line">    <span class="built_in">edge</span>()&#123;v=w=nxt=<span class="number">0</span>;&#125;</span><br><span class="line">    <span class="built_in">edge</span>(<span class="type">int</span> _v,<span class="type">int</span> _w,<span class="type">int</span> _nxt)&#123;v=_v;w=_w;nxt=_nxt;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">graph</span>&#123;</span><br><span class="line">    <span class="type">static</span> <span class="type">const</span> <span class="type">int</span> MAXN=<span class="number">500</span>,MAXM=<span class="number">5000</span>;</span><br><span class="line">    edge g[MAXM*<span class="number">2</span>+<span class="number">5</span>];</span><br><span class="line">    <span class="type">int</span> tot,head[MAXN+<span class="number">5</span>];</span><br><span class="line">    <span class="built_in">graph</span>()&#123;tot=<span class="number">1</span>;&#125;</span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">insert_edge</span><span class="params">(<span class="type">int</span> u,<span class="type">int</span> v,<span class="type">int</span> w,<span class="type">int</span> num)</span></span>&#123;</span><br><span class="line">        g[++tot]=<span class="built_in">edge</span>(v,w,head[u]);head[u]=tot;g[tot].num=num;</span><br><span class="line">        g[++tot]=<span class="built_in">edge</span>(u,<span class="number">0</span>,head[v]);head[v]=tot;g[tot].num=-num;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="type">int</span> lev[MAXN+<span class="number">5</span>],_head[MAXN+<span class="number">5</span>];</span><br><span class="line">    <span class="function"><span class="type">bool</span> <span class="title">bfs</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=n;i++)lev[i]=<span class="number">0</span>;</span><br><span class="line">        queue&lt;<span class="type">int</span>&gt; q;</span><br><span class="line">        q.<span class="built_in">push</span>(<span class="number">1</span>);lev[<span class="number">1</span>]=<span class="number">1</span>;</span><br><span class="line">        <span class="keyword">while</span>(!q.<span class="built_in">empty</span>())&#123;</span><br><span class="line">            <span class="type">int</span> u=q.<span class="built_in">front</span>();q.<span class="built_in">pop</span>();</span><br><span class="line">            <span class="keyword">for</span>(<span class="type">int</span> i=head[u];i;i=g[i].nxt)&#123;</span><br><span class="line">                <span class="type">int</span> v=g[i].v,w=g[i].w;</span><br><span class="line">                <span class="keyword">if</span>(!lev[v]&amp;&amp;w)&#123;</span><br><span class="line">                    q.<span class="built_in">push</span>(v);</span><br><span class="line">                    lev[v]=lev[u]+<span class="number">1</span>;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span>(lev[n])<span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">        <span class="keyword">else</span> <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="type">int</span> <span class="title">dfs</span><span class="params">(<span class="type">int</span> u,<span class="type">int</span> in)</span></span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(u==n)<span class="keyword">return</span> in;</span><br><span class="line">        <span class="type">int</span> out=<span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span>(<span class="type">int</span> &amp;i=_head[u];i;i=g[i].nxt)&#123;</span><br><span class="line">            <span class="type">int</span> v=g[i].v,w=g[i].w;</span><br><span class="line">            <span class="keyword">if</span>(w&amp;&amp;lev[v]==lev[u]+<span class="number">1</span>)&#123;</span><br><span class="line">                <span class="type">int</span> nxt=<span class="built_in">dfs</span>(v,<span class="built_in">min</span>(w,in));</span><br><span class="line">                <span class="keyword">if</span>(nxt)&#123;</span><br><span class="line">                    in-=nxt;</span><br><span class="line">                    out+=nxt;</span><br><span class="line">                    g[i].w-=nxt;</span><br><span class="line">                    g[i^<span class="number">1</span>].w+=nxt;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span>(out==<span class="number">0</span>)lev[u]=INF;</span><br><span class="line">        <span class="keyword">return</span> out;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="type">int</span> <span class="title">Dinic</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="type">int</span> res=<span class="number">0</span>;</span><br><span class="line">        <span class="keyword">while</span>(<span class="built_in">bfs</span>())&#123;</span><br><span class="line">            <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=n;i++)_head[i]=head[i];</span><br><span class="line">            <span class="keyword">while</span>(<span class="type">int</span> tmp=<span class="built_in">dfs</span>(<span class="number">1</span>,INF))&#123;</span><br><span class="line">                res+=tmp;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line">graph g;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">    ios::<span class="built_in">sync_with_stdio</span>(<span class="literal">false</span>);</span><br><span class="line">    cin&gt;&gt;n&gt;&gt;m;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=m;i++)&#123;</span><br><span class="line">        <span class="type">int</span> u,v,w;</span><br><span class="line">        cin&gt;&gt;u&gt;&gt;v&gt;&gt;w;u++;v++;</span><br><span class="line">        g.<span class="built_in">insert_edge</span>(u,v,w,i);</span><br><span class="line">    &#125;</span><br><span class="line">    g.<span class="built_in">Dinic</span>();</span><br><span class="line">    <span class="type">int</span> ans=<span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=m;i++)&#123;</span><br><span class="line">        g.g[(i&lt;&lt;<span class="number">1</span>)].w++;</span><br><span class="line">        <span class="keyword">if</span>(g.<span class="built_in">bfs</span>())ans++;</span><br><span class="line">        g.g[(i&lt;&lt;<span class="number">1</span>)].w--;</span><br><span class="line">    &#125;</span><br><span class="line">    cout&lt;&lt;ans&lt;&lt;endl;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2022/05/18/%E9%A2%98%E8%A7%A3%20AcWing%202236.%20%E4%BC%8A%E5%9F%BA%E7%9A%84%E6%95%85%E4%BA%8B%20I%20-%20%E9%81%93%E8%B7%AF%E9%87%8D%E5%BB%BA/" data-id="clk9o8lna000l8wi5c2me340z" data-title="题解 AcWing 2236. 伊基的故事 I - 道路重建" class="article-share-link"><span class="fa fa-share">Share</span></a>
      
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2022/05/18/%E7%9C%81%E9%80%89%E6%B8%B8%E8%AE%B0%EF%BC%9F%E7%9C%81%E9%80%89%E6%B8%B8%E5%AF%84%EF%BC%81/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          省选游记？省选游寄！
        
      </div>
    </a>
  
  
    <a href="/2022/02/17/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%20%E7%BD%91%E7%BB%9C%E6%B5%81/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">学习笔记 网络流</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2023/07/">July 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/10/">October 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/05/">May 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/02/">February 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/01/">January 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/08/">August 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/07/">July 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/06/">June 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/08/">August 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2023/07/17/%E5%86%B3%E7%AD%96%E5%8D%95%E8%B0%83%E6%80%A7%E4%BC%98%E5%8C%96DP%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%20AND%20P4767%20%E3%80%8CIOI2000%E3%80%8D%20%E9%82%AE%E5%B1%80%20%E9%A2%98%E8%A7%A3/">决策单调性优化DP 学习笔记 AND P4767 「IOI2000」 邮局 题解</a>
          </li>
        
          <li>
            <a href="/2022/10/30/CSP-S%202022%20%E6%B8%B8%E8%AE%B0/">CSP-S 2022 游记</a>
          </li>
        
          <li>
            <a href="/2022/05/29/%E6%96%9C%E7%8E%87%E4%BC%98%E5%8C%96%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">斜率优化动态规划 学习笔记</a>
          </li>
        
          <li>
            <a href="/2022/05/22/PKUSC%E4%B8%80%E5%8F%A5%E8%AF%9D%E6%B8%B8%E8%AE%B0/">PKUSC一句话游记</a>
          </li>
        
          <li>
            <a href="/2022/05/18/%E7%9C%81%E9%80%89%E6%B8%B8%E8%AE%B0%EF%BC%9F%E7%9C%81%E9%80%89%E6%B8%B8%E5%AF%84%EF%BC%81/">省选游记？省选游寄！</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2023 John Doe<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/js/jquery-3.6.4.min.js"></script>



  
<script src="/fancybox/jquery.fancybox.min.js"></script>




<script src="/js/script.js"></script>





  </div>
</body>
</html>